<?php

namespace Modules\Order\Http\Controllers\Admin\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Modules\Order\Entities\UnionOrder;
use Modules\Product\Entities\Sku;

use Modules\Order\Entities\BaseOrder as Order;

use Modules\Asset\Entities\PackageOrder;

use Validator;
use ThrowException;
use Auth;
use Payment;
use DB;

class OrderController extends Controller
{
    public function index(Request $req)
    {
        $rule = [
        ];

        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $options = $req->all();
        $options['where'][] = ['user_id', '>', '500'];
        $options['with'] = ['skus', 'address', 'user', 'deliver_record', 'aftersale'];

        // $options['scopes'] = ['displayAtList'];

        $options['only'] = [
            'id',
            'uuid',
            'number',
            'client_name',
            'client_type',
            'pay_type',
            'user_remark',
            'created_at',
            'offline_use_at',
            'order_money_price',
            'order_score_price',
            'pay_money_price',
            'pay_score_price',
            'carriage',
            'union_status',
            'union_status_text',
            'activity_type_text',
            'node_type_text',
            'closed_reason',
            'closed_at',
            'remark',
            'seller_remark',
            'coupon_discount',
            'redpack_discount',
            'score_discount',
            'score_used',
            'miniapp_type',
            'cover_type',
            'cover_discount',
            'multi_buy_money_discount',
            'multi_buy_score_discount',
            'exchange_record_id',
            'exchange_score_discount',
            'exchange_money_discount',
            'type',
            'supplier',
            'aftersale',
            'skus' => [
                '*' => [
                    'title',
                    'number',
                    'supplier',
                    'thumb',
                    'money_price',
                    'score_price',
                    'discount_money_price',
                    'discount_score_price',
                    'group_price_title',
                    'node_type_text',
                    'pay_order_number',
                    'attrs',
                    'total',
                    'options'
                ]
            ],
            'address',
            'marked_at',
            'user' => [
                'id',
                'uuid',
                'name',
                'client_type',
                'headimg',
                'phone'
            ],
            'deliver_record' => [
                'id',
                'uuid',
                'type',
                'express_name',
                'express_number',
                'content',
                'remark',
                'created_at',
                'updated_at'
            ]
        ];

        $list = Order::getList($options);

        return $list->generateListResponse();
    }
    //推送供应链
    public function tuisongGongYingLian(){
        
    }
    public function update(Request $req, $uuid)
    {
        $rule = [
            'type' => 'required',
        ];

        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $order = Order::getEntity([
            'uuid' => $uuid
        ]);

        DB::beginTransaction();

        if ($req->type === 'deliver') {
            $order->deliver($req->input('deliver_info'));
        }
        elseif ($req->type === 'update_seller_remark')
            $order->update([
                'seller_remark' => $req->input('seller_remark')
            ]);
        elseif ($req->type === 'update_address') {
            $order->address->update($req->input('address'));
        }
        elseif ($req->type === 'update_deliver_info') {
            $order->updateDeliverInfo($req->input('deliver_info'));
        }
        elseif ($req->type === 'mark')
            $order->update([
                'marked_at' => date('Y-m-d H:i:s')
            ]);
        elseif ($req->type === 'unmark')
            $order->update([
                'marked_at' => null
            ]);
        elseif ($req->type === 'close_and_refund') {
            $refundMoney = $req->input('refund_money');
            $refundBalance = $req->input('refund_balance', 0);    // 退回到帐户余额
        	$order->closeAndRefund(2, '管理后台关闭', [
                'refund_money' => $refundMoney,
                'refund_balance' => $refundBalance
            ]);
        }
        elseif ($req->type === 'cancel_refund')
            $order->cancelRefund();
        elseif ($req->type === 'part_refund') {
            $money = intval($req->input('money') * 100);
            $order->refund($money);
            $str = '('. date('Y-m-d H:i:s') . '退款' . ($money / 100) .'元)';
            $order->update([
                'seller_remark' => $order->seller_remark . $str
            ]);
        }
        elseif ($req->type === 'reset_to_package') {

            if ($order->type !== 'package') {
                ThrowException::Conflict('此订单不是提货订单，无法撤回仓库!');
            }

            $order = PackageOrder::getEntity([
                'id' => $order->id
            ]);

            // 撤回到物品仓库
            $order->resetToPackage();
        }

        DB::commit();

        return [];
    }
}
